/*----------------------------------------------
| noka select v1.0 www.97521.com                 |
| rebin 2014-01-15                             |
|---------------------------------------------*/
var nselectdouble = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.cfg = cfg;					//配置信息
    	this.id=cfg.id; //id
    	this.repeatCheck=cfg.repeatCheck;
    	this.lang=cfg.lang;
    	this.readonly=cfg.readonly;
    	this.buttionClass=cfg.buttionClass;
    	this.left = cfg.left;
    	this.right=cfg.right;
    	this.leftJson=cfg.leftJson || [];
    	this.rightJson=cfg.rightJson || [];
    },
    show : function(){
    	this.bodycreate();
    },
    bodycreate : function(){
    	var self = this;
    	var idx = 0;
        var html = [];
        html[idx++] = '<table  border="1" cellpadding="0" cellspacing="0" bordercolor="#DCA485">';
        html[idx++] = '<tr>';
        html[idx++] = '<td bgcolor="#C7C2BE" align="center">'+self.lang.left+'</td>';
        html[idx++] = '<td bgcolor="#C7C2BE" align="center">'+self.lang.center+'</td>';
        html[idx++] = '<td bgcolor="#C7C2BE" align="center">'+self.lang.right+'</td>';
        html[idx++] = '</tr>';
        html[idx++] = '<tr><td><select name="'+self.left.name+'"  multiple="multiple" style="width:'+self.left.width+';height:'+self.left.heigth+'" id="'+self.left.id+'"></select></td>';
        //---------------------------------------------------------------------
        html[idx++] = '<td width="61px" align="center"><table width="45px" height="97px" border="0" cellpadding="0" cellspacing="0">';
    	html[idx++] = '<tr><td align="center"><input id="'+self.id+'_b1" class="'+self.buttionClass+'" type="button" name="Submit" value="&gt;" style="width:30px"/></td></tr>';
    	html[idx++] = '<tr><td align="center"><input id="'+self.id+'_b2" class="'+self.buttionClass+'" type="button" name="Submit2" value="&lt;" style="width:30px"/></td></tr>';
    	html[idx++] = '<tr><td align="center"><input id="'+self.id+'_b3" class="'+self.buttionClass+'" type="button" name="Submit3" value="&gt;&gt;" style="width:30px"/></td></tr>';
    	html[idx++] = '<tr><td align="center"><input id="'+self.id+'_b4" class="'+self.buttionClass+'" type="button" name="Submit4" value="&lt;&lt;" style="width:30px"/></td></tr>';
    	html[idx++] = '<tr><td align="center"><input id="'+self.id+'_b5" class="'+self.buttionClass+'" type="button" name="Submit42" value="&lt;&and;" style="width:30px"/></td></tr>';
    	html[idx++] = '<tr><td align="center"><input id="'+self.id+'_b6" class="'+self.buttionClass+'" type="button" name="Submit422" value="&lt;&or;" style="width:30px"/></td></tr>';
    	html[idx++] = '<tr><td align="center"><input id="'+self.id+'_b7" class="'+self.buttionClass+'" type="button" name="Submit423" value="&and;&gt;" style="width:30px"/></td></tr>';
    	html[idx++] = '<tr><td align="center"><input id="'+self.id+'_b8" class="'+self.buttionClass+'" type="button" name="Submit4222" value="&or;&gt;" style="width:30px"/></td></tr>';
    	html[idx++] = '</table></td>';
    	html[idx++] = '<td><select name="'+self.right.name+'"  multiple="multiple" style="width:'+self.right.width+';height:'+self.right.heigth+'" id="'+self.right.id+'"></select></td>';
    	html[idx++] = '</tr>';
    	html[idx++] = '</table>';
    	$(self.id).innerHTML=html.join('');
    	self.setLeftOptions(self.leftJson);
    	self.setRightOptions(self.rightJson);
    	self.ExEvents();
    	if(!self.readonly){
    		self.selectEvent();
    	}
    	
    },
    ExEvents : function(){
    	var self = this;
    	$(self.id).setLeftOptions= function(Ljson){
    		self.setLeftOptions(Ljson);
    	};
    	$(self.id).setRightOptions= function(Rjson){
    		self.setRightOptions(Rjson);
    	};
    },
    setLeftOptions : function(Ljson){
    	var self = this;
    	var leftobj = $(self.left.id);
    	for(i=0;i<Ljson.length;i++){
    		leftobj.options[i]=new Option(Ljson[i].text,Ljson[i].value);
    	}
    },
    setRightOptions : function(Rjson){
    	var self = this;
    	var rightobj = $(self.right.id);
    	for(i=0;i<Rjson.length;i++){
    		rightobj.options[i]=new Option(Rjson[i].text,Rjson[i].value);
    	}
    },
    //=======================================================
    selectEvent : function(){
    	var self = this;
    	$(self.left.id).observe('dblclick',function(a){
    		self.ondocublesnet(self.left.id,self.right.id);
    	});
    	$(self.right.id).observe('dblclick',function(a){
    		self.ondocublesnetdel(self.right.id);
    	});
    	$(self.id+'_b1').observe('click',function(a){
    		self.LietToRight(self.left.id,self.right.id);
    	});
    	$(self.id+'_b2').observe('click',function(a){
    		self.RightToLiet(self.left.id,self.right.id);
    	});
    	$(self.id+'_b3').observe('click',function(a){
    		self.LietAllToRight(self.left.id,self.right.id);
    	});
    	$(self.id+'_b4').observe('click',function(a){
    		self.RightAllToLiet(self.left.id,self.right.id);
    	});
    	$(self.id+'_b5').observe('click',function(a){
    		self.MoveSelectedOptionsUp(self.left.id);
    	});
    	$(self.id+'_b6').observe('click',function(a){
    		self.MoveSelectedOptionsDown(self.left.id);
    	});
    	$(self.id+'_b7').observe('click',function(a){
    		self.MoveSelectedOptionsUp(self.right.id);
    	});
    	$(self.id+'_b8').observe('click',function(a){
    		self.MoveSelectedOptionsDown(self.right.id);
    	});
    },
    //----------- 左选框向右选框加数据项-------------	
    LietToRight : function(liet,right){
    	var liet = $(liet);
    	var right = $(right);
    	var i=0;
    	while(i<liet.options.length){
    		if(liet.options[i].selected){
    			if(!this.OptionExists(right,liet.options[i].text,liet.options[i].value,this.repeatCheck)){
    				right.options[right.options.length]=new Option(liet.options[i].text, liet.options[i].value);
    			}else{
    				alert(this.lang.chek);
    			}
    			}
    		i++;
    		}
    	},
    	//----------- 右选框删除数据项 -----------------
    	RightToLiet : function (liet,right){
    		var liet = $(liet);
    		var right = $(right);
    		var i=0;
    		while(i<right.options.length){
    			if(right.options[i].selected){
    				right.options[i]=null;
    				}else{
    					i++;
    				}
    			}
    		},
    	//------------- 左边全部数据到右边框-------------
    	LietAllToRight : function (liet,right){
    		var liet = $(liet);
    		var right = $(right);
    		var i=0;
    		while(i<liet.options.length){
    			if(!this.OptionExists(right,liet.options[i].text,liet.options[i].value,this.repeatCheck)){
    				right.options[right.options.length]=new Option(liet.options[i].text, liet.options[i].value);
    				}else{
    					alert(this.lang.chek);
    				}
    			i++;
    			}
    	},
    	//------------- 右边全部数据到左边框-------------	
    	RightAllToLiet : function(liet,right){
    		var liet = $(liet);
    		var right = $(right);
    		var i=0;
    		while(i<right.options.length){
    			right.options[i]=null;
    		}
    	},
    	//--------------- 列表中选中项上移 ----------------
    	MoveSelectedOptionsUp : function(listv) {
    		var list = $(listv);
    		var i = 0;
    		var value = "";
    		var text = "";
    		for (i=0; i<(list.options.length-1); i++) {
    			if (!list.options[i].selected && list.options[i+1].selected){
    				value = list.options[i].value;
    				text = list.options[i].text;
    				list.options[i] = new Option(list.options[i+1].text, list.options[i+1].value);
    				list.options[i].selected = true;
    				list.options[i+1] = new Option(text, value);
    				}
    			}
    	},
    	//--------------- 列表中选中项下移 ----------------
    	MoveSelectedOptionsDown : function (listv) {
    		var list = $(listv);
    		var i = 0;
    		var value = "";
    		var text = "";
    		for (i=list.options.length-1; i>0; i--) {
    			if (!list.options[i].selected && list.options[i-1].selected){
    				value = list.options[i].value;
    				text = list.options[i].text;
    				list.options[i] = new Option(list.options[i-1].text, list.options[i-1].value);
    				list.options[i].selected = true;
    				list.options[i-1] = new Option(text, value);
    				}
    			}
    	},
    	//--------------- 左选择框双击事件处理方法  ----------------
    	ondocublesnet : function(liet, right) {
				var liet = $(liet);
				var right = $(right);
				try {
					if (!this.OptionExists(right,liet.options[liet.selectedIndex].text,liet.options[liet.selectedIndex].value, 'v')) {
						right.options[right.options.length] = new Option(liet.options[liet.selectedIndex].text,liet.options[liet.selectedIndex].value);
					} else {
						alert(this.lang.chek);
					}
				} catch (e) {
				}
		},
		//--------------- 右选择框双击事件处理方法  ----------------
		ondocublesnetdel : function(right) {
				try {
					var right = $(right);
					right.options[right.selectedIndex] = null;
				} catch (e) {
				}
		},
		//--------------- 检查列表中的重复项 ----------------														
		OptionExists : function(list, optText, optValue, repeatCheck) {
				var i = 0;
				var find = false;
				if (repeatCheck == "v") {
					//--------按值进行重复值检查-------------------
					for (i = 0; i < list.options.length; i++) {
						if (list.options[i].value == optValue) {
							find = true;
							break;
						}
					}
				} else if (repeatCheck == "t") {
					//按文字进行重复检查
					for (i = 0; i < list.options.length; i++) {
						if (list.options[i].text == optText) {
							find = true;
							break;
						}
					}
				} else if (repeatCheck == "vt") {
					// 按值和文字进行重复检查
					for (i = 0; i < list.options.length; i++) {
						if ((list.options[i].value == optValue)
								&& (list.options[i].text == optText)) {
							find = true;
							break;
						}
					}
				}
				return find;
		}
 });